package com.project.controller;

import com.project.bean.UserBean;
import com.project.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private IUserService userService;

    @RequestMapping("login")
    public String login(String code, String pwd, HttpServletRequest request){
        UserBean user = userService.login(code,pwd);
        if (user==null){
            return "no";
        }
        request.getSession().setAttribute("loginUser",user);
        return "ok";
    }

    @RequestMapping("register")
    public String addUser(String code,String mail,String face){
        UserBean user = new UserBean();
        user.setCode(code);
        user.setMail(mail);
        user.setFace("/html"+face);
        if (userService.addUser(user)){
            return "ok";
        }
        return "no";
    }


    @RequestMapping("getUser")
    public UserBean getUserName(HttpServletRequest request){
        UserBean user = (UserBean)request.getSession().getAttribute("loginUser");
        return user;
    }
}
